package pk.nak.adapters;

import java.util.ArrayList;

import pk.nak.client.R;
import pk.nak.client.activity.PriceComparisonActivity;
import pk.nak.data.EatOutMenuItem;
import pk.nak.enums.ExtraIDs;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;

public class MenuItemAdapter extends ArrayAdapter<EatOutMenuItem> {

	private ArrayList<EatOutMenuItem> items;
	public MenuItemAdapter(Context context, int textViewResourceId,
			ArrayList<EatOutMenuItem> objects) {
		super(context, textViewResourceId, objects);
		this.items = objects;
		// TODO Auto-generated constructor stub
	}

	private View.OnClickListener compareOnClick = new View.OnClickListener() {
		public void onClick(View v) {
			// TODO Auto-generated method stub
			EatOutMenuItem eomi = (EatOutMenuItem)v.getTag();
			Intent priceComparisonIntent = new Intent(getContext(), PriceComparisonActivity.class);
			priceComparisonIntent.putExtra(ExtraIDs.SELECTED_MENUITEM_SERIALIZABLE, eomi);
			getContext().startActivity(priceComparisonIntent);
			
			//Toast.makeText(v.getContext(), eomi.getTitle(), Toast.LENGTH_SHORT).show();
		}
	};
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		//return super.getView(position, convertView, parent);
		View v = convertView;
		if ( v == null ){
			LayoutInflater vi = (LayoutInflater)this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			v = vi.inflate(R.layout.eatoutmenuitemrow, null);
		}
		
		EatOutMenuItem eomi = this.items.get(position);
		
		if ( eomi != null ){
			TextView tvName = (TextView) v.findViewById(R.id.tvMenuItemName);
			TextView tvPrice = (TextView) v.findViewById(R.id.tvMenuItemPrice);
			Button btnCompare = (Button) v.findViewById(R.id.btnCompare);
			
			if ( tvName != null ) tvName.setText(eomi.getTitle());
			if ( tvPrice != null ) tvPrice.setText(""+eomi.getPrice());
			if ( btnCompare != null ){
				btnCompare.setTag(eomi);
				btnCompare.setOnClickListener(this.compareOnClick);
			}
		}
		return v;
	}

}
